package org.adorsys.aderp.aderplogin.client;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.adorsys.aderp.aderplogin.domain.AderpClientDetail;
import org.adorsys.aderp.aderplogin.domain.AderpUser;
import org.adorsys.aderp.aderplogin.domain.ClientResource;
import org.adorsys.aderp.aderplogin.domain.ClientRole;
import org.adorsys.aderp.aderplogin.domain.ClientScope;
import org.adorsys.aderp.aderplogin.domain.UserRole;
import org.springframework.security.oauth2.provider.ClientDetails;

public class ClientDetailsHelper {
	
	public static ClientDetails createClientDetails(AderpClientDetail clientDetail){
		String commaSeparatedResourceIds = toCsvClientResource(clientDetail.getClientResources());
		String commaSeparatedScopes = toCsvClientScope(clientDetail.getClientScopes());
		String commaSeparatedAuthorizedGrantTypes = clientDetail.getAuthorizedGrantTypes();
		String commaSeparatedAuthorities = toCsvClientRole(clientDetail.getClientRoles());
		AderpBasicClientDetails baseClientDetails = new AderpBasicClientDetails(
				clientDetail.getClientId(),clientDetail.getClientSecret(), clientDetail.getRegisteredRedirectUri(),
				commaSeparatedResourceIds, commaSeparatedScopes,
				commaSeparatedAuthorizedGrantTypes, commaSeparatedAuthorities);
		return baseClientDetails;
	}

	private static String toCsvClientResource(Collection<ClientResource> c){
		String result = null;
		for (ClientResource cr : c) {			
			if(result==null) {
				result = cr.getResKey();
			} else {
				result+=","+cr.getResKey();
			}
		}
		return result;
	}
	private static String toCsvClientScope(Collection<ClientScope> c){
		String result = null;
		for (ClientScope cr : c) {			
			if(result==null) {
				result = cr.getScopeKey();
			} else {
				result+=","+cr.getScopeKey();
			}
		}
		return result;
	}
	
	private static String toCsvClientRole(Collection<ClientRole> c){
		String result = null;
		for (ClientRole cr : c) {			
			if(result==null) {
				result = cr.getRoleKey();
			} else {
				result+=","+cr.getRoleKey();
			}
		}
		return result;
	}
	
	public static Set<UserRole> getSupportedRoles(AderpUser aderpUser, AderpClientDetail aderpClientDetail){
		String rolePrefixes = aderpClientDetail.getRolePrefixes();
		Set<UserRole> roleNames = aderpUser.getRoleNames();
		if(rolePrefixes==null) return roleNames;
		String[] split = rolePrefixes.split(",");
		Set<UserRole> result = new HashSet<UserRole>();
		for (String rolePrefix : split) {
			for (UserRole userRole : roleNames) {
				if(userRole.getPermProfile().contains(rolePrefix)){
					result.add(userRole);
				}
			}			
		}
		return result;
	}
	
	public static UserRole getUserRole(AderpUser aderpUser, AderpClientDetail aderpClientDetail, UserRole candidate){
		if(candidate==null) return null;
		Set<UserRole> supportedRoles = getSupportedRoles(aderpUser, aderpClientDetail);
		for (UserRole userRole : supportedRoles) {
			if(userRole.getRoleKey().equals(candidate.getRoleKey())) return userRole;
		}
		return null;
	}
	
}
